package com.example.takephotovideo.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.takephotovideo.R;
import com.example.takephotovideo.adapter.ShowPhotoVideoAdapter;
import com.example.takephotovideo.takephotovideo.PhotoVideoListBean;
import com.example.takephotovideo.utils.RecyclerViewTieku;

import java.util.ArrayList;
import java.util.List;

/**
 * @功能:
 * @Creat 2020/3/18 18:45
 * @User Lmy
 * @Compony JinAnChang
 */
public class ShowPhotoView extends RelativeLayout {
    View inflate;
    private RecyclerView recyclerview;
    private List<PhotoVideoListBean> fileListBeans = new ArrayList<>();
    GridLayoutManager gridLayoutManager;
    private RecyclerViewTieku myItemDecoration;
    private ShowPhotoVideoAdapter adapter;
    private Context context;

    private int linNumber;//一行显示几列

    public ShowPhotoView(Context context) {
        this(context, null);
    }

    public ShowPhotoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        // 获取属性集合 TypedArray
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShowPhotoView);
        linNumber = typedArray.getInteger(R.styleable.ShowPhotoView_lineNumber, 3);
        // 用完要关闭回收资源，必须的强制性的
        typedArray.recycle();
        inflate = LayoutInflater.from(context).inflate(R.layout.showphotoview, this, true);
        recyclerview = findViewById(R.id.recyclerview);
        myItemDecoration = new RecyclerViewTieku.Builder(context).
                setHorizontalSpan(5)
                .setVerticalSpan(5)
                .setColorResource(R.color.white)
                .setShowLastLine(false)//是否显示左后一行的分割线
                .build();
        gridLayoutManager = new GridLayoutManager(context, linNumber);
        initRecyclerview();
    }

    private void initRecyclerview() {
        if (adapter == null) {
            adapter = new ShowPhotoVideoAdapter(context, fileListBeans);
            recyclerview.setLayoutManager(gridLayoutManager);
//            recyclerview.addItemDecoration(myItemDecoration);
//                recyclerview.setItemAnimator(new DefaultItemAnimator());
            recyclerview.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 设置数据源
     */
    public void setDataList(List<PhotoVideoListBean> fileListBean) {
        this.fileListBeans.addAll(fileListBean);
        initRecyclerview();
    }
}
